(function($){
$.ajaxTransport("+*", function(s){    
  var xhr;
  if (s.XHR)
    return {
      send: function(headers, complete){   
        xhr = s.xhr();
        xhr.open( s.type, s.url, s.async );
        delete headers["Content-Type"];
        headers[ "X-Requested-With" ] = "XMLHttpRequest";
    
        for ( i in headers ) {
         xhr.setRequestHeader( i, headers[ i ] );
        }
  	
      	var callback = function(e){
      	  var responses = {xml: xhr.responseXML, text: xhr.responseText};
      	  complete( xhr.status, xhr.statusText, responses, xhr.getAllResponseHeaders() );
      	};
  	
      	xhr.addEventListener("load", callback);
      	xhr.addEventListener("error", callback);

        xhr.send(s.data);
        
      },
  
      abort: function(){
        if (xhr) xhr.abort();
      }
  };
});

var defaultSettings = {
	processData: false,
	contentType: false,
	type:        "POST",
	XHR:      true
};

function filename(filePath) {
	return filePath.replace(/.*(\/|\\)/, "");
}

function TranslateURL(src){
	//转换img的路径
	return src.substring(1,src.length);
}

var MultiUpload = MultiUpload || {};

MultiUpload = {
	init:function(){
		this._initUpload("input#uploadfile");
		this.bindEvent();
	},
	_events:{
		
	},
	
	DisableInput:function(element){
		$(element).attr("disabled",true);
	},
	
	checksize:function(file){
		//未写完
	},
	
	bindEvent:function(){
		
	},
	
	_initUpload:function(element){
		var self = this;
		$(element).ajaxPhotoInfo({
			onChange: self.Change,
			onComplete: self.Complete
		});
	},
	
	Error:function(){
		
	},
	
	Change:function(e){
		var element	= $(e.target),
            file	= filename(element.val()),
            form = $("#upload");
        //判断大小？
        MultiUpload.checksize(file);
		form.submit();
		//如何提示正在加载？？
		//$("#Upload-img").append('');   //提示正在加载
	},
	Complete:function(e){
		//取消正在加载？？
		var iframe  = $("iframe[name=result]"),
            doc = iframe[0].contentDocument ? iframe[0].contentDocument : window.frames[iframe[0].name].document,
            response = doc.body.innerHTML;
        if(response){
        	var json = eval("(" + response + ")");
        	var target = $("#img-area");
			if(json.success == "true"){
				for(var e in json.data){
					var text ="<div pid = '"+json.data[e].uid+"' class = 'content_d_list'>";  //<div class="content_d_list" pid="a">
					text +="<div class='content_list_left'>"; // <div class="content_list_left">
					text += "<img src='"+ TranslateURL(json.data[e].url)+"' >";  //<img src="images/img.jpg">
					text += "</div>";    //</div>
					text += "<div class='content_list_right'>";  //<div class="content_list_right">
					text += "<div id = 'Issue-info-s'>"; //<div id = "Issue-info-s">
					text += "<div style='height: 150px;'>"; //<div style="height: 150px;">
					text += "<div id = 'Issue-info-style-s'>";  //<div id = "Issue-info-style-s">
					text += "<div style='margin-bottom: 15px'> 分类<font color=Red>*</font>";  //<div style="margin-bottom: 15px"> 分类<font color=Red>*</font>
					text += "<p style='display:inline;margin-left:5px; color:#00ca00'>";  //<p style='display:inline;margin-left:5px; color:#00ca00'>
					text += "(两种分类方式至少选择一种)";
					text += "</p></div>";  //</p></div>
					text += "<div>";  //<div>
					text += "<span class='select'> 按内容：</span>"; //<span class='select'> 按内容：</span>
					text += "<div class='classify' id = 'dropdown-cont' style='float: left;height: 40px;'>"; //<div class='classify' id = "dropdown-cont" style="float: left;height: 40px;">
					text += "<ul class='nav nav-pills'>"; //<ul class="nav nav-pills">
					text +=  " <li class='dropdown'>"; // <li class="dropdown">
					text += "<a class='dropdown-toggle' data-toggle='dropdown' href='#' tagid = '1'>按内容 <b class='caret'></b></a>";  //<a class="dropdown-toggle" data-toggle="dropdown" href="#" tagid = "1">按内容 <b class="caret"></b></a>
					text += "<ul class='dropdown-menu'>";//<ul class="dropdown-menu">
					text += "</ul> </li></ul>";  //</ul> </li></ul>
					text += "</div>";  //</div>
					text += "<div style='clear: both'></div>";  //<div style="clear: both"></div>
					text += "</div>"; // </div>
					text += "<div>"; // <div>
					text += "<span class='select'> 按风格：</span>";  //<span class='select'> 按风格：</span>
					text += "<div class='classify' id = 'dropdown-style'  style='float: left;height: 36px;'>"; //<div class='classify' id = "dropdown-style"  style="float: left;height: 36px;">
					text += "<ul class='nav nav-pills'>"; //<ul class="nav nav-pills">
					text +=  " <li class='dropdown'>"; // <li class="dropdown">
					text += "<a class='dropdown-toggle' data-toggle='dropdown' href='#' tagid = '0'>按风格 <b class='caret'></b></a>";  //<a class="dropdown-toggle" data-toggle="dropdown" href="#" tagid = "0">按风格 <b class="caret"></b></a>
					text += "<ul class='dropdown-menu'>";//<ul class="dropdown-menu">
					text += "</ul> </li></ul>";  //</ul> </li></ul>
					text += "</div>";  //</div>
					text += "<div style='clear: both'></div>";  //<div style="clear: both"></div>
					text += "</div>"; // </div>
					text += "</div>";  //</div>
					text += "<div id='Issue-info-desp-s'>"; //<div id="Issue-info-desp-s">
					text += "<div id='album-position-s'> ";  //<div id="album-position-s"> 
					text += "<span>拍摄地点：<input id = 'text-position' style='height:22px' />";  //<span>拍摄地点：<input id = "text-position" style='height:22px' value=''/>
					text += "<p style='display:inline;color:#00ca00'>";  //<p style='display:inline;color:#00ca00'>
					text += "例如 ：湖北 武汉";  //例如 ：湖北 武汉
					text += "</p></span></div>"; //</p></span></div>
					text += "<div class='description'>"; //<div class='description'>
					text += "<p>";  //<p>
					text += "照片描述：";  //照片描述：
					text += "</p> ";//</p>
					text += "<textarea type='text' id = 'text-descrip' wrap='virtual' maxlength=500 value='字数不多于500' style='font-size:10px;color:#8c8c8c;height:70px;width:400px;'></textarea>";  //<textarea type="text" id = "text-descrip" wrap="virtual" maxlength=500 value="字数不多于500" style="font-size:10px;color:#8c8c8c;height:70px;width:400px;"></textarea>
					text += "</div></div>";  //</div></div>
					text +="<div style='clear: both'></div>";  //<div style="clear: both"></div>
					text += "</div></div></div>"; //</div></div></div>
					text +="<div style='clear: both'></div>";  //<div style="clear: both"></div>
					text += "</div>";  //</div>
					 
					
					//插入到Html中
					target.append(text);
					BindImgEvent();
					BindTypeData();
					//disable fileinput
					//MultiUpload.DisableInput("input#uploadfile");
				};
			}
			else{
				//显示加载失败
				MessageShow("加载图片失败，请重试");
			}
			
        }
	},
	
	UpdataImg:function(e){
		
	}
	
	
};



$.fn.ajaxPhotoInfo = function(options){
	var el = this;
	var defaults = {
		onChange:function(e){},
		onComplete:function(e){},
		onError:function(e){}
	};
	var defaultSettings = $.extend({}, defaults, options);
	this.each(function(){
		var $this = $(this);
		if($this.is("input") && $this.attr("type") === "file"){
			$this.bind("change",defaultSettings.onChange);
		}
	});
	$("iframe[name=result]").bind('load', defaultSettings.onComplete);
	
}

MultiUpload.init();

})(jQuery)
